package com.itcoon.cloud.framework.webmvc.response;

import com.itcoon.cloud.framework.enums.CommonResponseCode;
import com.itcoon.cloud.framework.enums.ExceptionTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;

import java.io.Serializable;

/**
 * <p> </p>
 *
 * @author 俞淼霞
 * @since 2019-09-03
 */
@Schema
public class ApiResponse<T> extends AbstractResponse<T> implements Serializable {

    private static final long serialVersionUID = -7416528189137018359L;
    protected static final ExceptionTypeEnum EXCEPTION_TYPE_SYSTEM = ExceptionTypeEnum.SYSTEM;
    protected static final ExceptionTypeEnum EXCEPTION_TYPE_OK = ExceptionTypeEnum.OK;

    public ApiResponse() {
    }

    public static ApiResponse<Void> ok() {
        return ok(null);
    }

    public static <T> ApiResponse<T> ok(T t) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setCode(CommonResponseCode.OK.getCode());
        response.setMessage(CommonResponseCode.OK.getMessage());
        response.setData(t);
        return response;
    }

    public static ApiResponse<Void> error() {
        CommonResponseCode responseCode =  CommonResponseCode.SYSTEM_EXCEPTION;
        ApiResponse<Void> response = new ApiResponse<>();
        response.setCode(responseCode.getCode());
        response.setMessage(responseCode.getMessage());
        return response;
    }

    public static <T> ApiResponse<T> error(String code, String message) {
        return error(code, message, null);
    }

    public static <T> ApiResponse<T> error(String code, String message, T t) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setCode(code);
        response.setMessage(message);
        response.setData(t);
        return response;
    }

}
